<template>
  <div>

    <el-dialog :title="title" :visible.sync="open" :close-on-click-modal="false" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">

            <el-row>
              <el-col :span="12">
                <el-form-item label="姓　　名" prop="name">
                  <el-input v-model="form.name" placeholder="请输入姓名" :disabled="this.form.id != null" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="身份证号" prop="idNo">
                  <el-input v-model="form.idNo" placeholder="请输入身份证号" :disabled="this.form.id != null" />
                </el-form-item>
              </el-col>

            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item label="联系电话" prop="phone">
                  <el-input v-model="form.phone" placeholder="请输入联系电话" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="民　　族" prop="nation">
                  <el-select v-model="form.nation" placeholder="请选择" style="width: 100%;">
                    <el-option
                      v-for="nation in nationOptions"
                      :key="nation.name"
                      :label="nation.name"
                      :value="nation.name"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item label="性　　别">
                  <el-select v-model="form.sex" placeholder="请选择" style="width: 100%;" :disabled="true">
                    <el-option
                      v-for="dict in dict.type.sys_user_sex"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="年　　龄" prop="age">
                  <el-input type="number" v-model="form.age" placeholder="请输入年龄" :disabled="true"/>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item label="文化程度" prop="degreeName">
                  <el-select v-model="form.degreeName" placeholder="请选择" style="width: 100%;">
                    <el-option
                      v-for="dict in dict.type.sys_degree_name"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="政治面貌" prop="politicalStatus">
                  <el-select v-model="form.politicalStatus" placeholder="请选择" style="width: 100%;">
                    <el-option
                      v-for="dict in dict.type.sys_political_status"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item label="籍　　贯" prop="nativePlace">
                  <el-select v-model="form.nativePlace" placeholder="请选择" style="width: 100%;">
                    <el-option
                      v-for="province in provinceOptions"
                      :key="province.name"
                      :label="province.name"
                      :value="province.name"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="血　　型"  prop="bloodType">
                  <el-select v-model="form.bloodType" placeholder="请选择" style="width: 100%">
                    <el-option
                      v-for="dict in dict.type.sys_blood_type"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item label="紧急联系人" prop="urgentContact">
                  <el-input v-model="form.urgentContact" placeholder="请输入紧急联系人" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="联系人电话" prop="urgentTel">
                  <el-input v-model="form.urgentTel" placeholder="请输入联系人电话" />
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="24">
                <el-form-item label="住　　址" prop="address">
                  <el-input v-model="form.address" type="textarea" placeholder="请输入住址"></el-input>
                </el-form-item>
              </el-col>
            </el-row>

      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>

    </el-dialog>

  </div>
</template>

<script>
  import { getNationList, getProvinceList } from "@/api/common/dict";
  import { addStaff, getStaff, updateStaff } from "@/api/site/labour/staff";
  import { validIdNo, validPhone } from "@/api/common/validate";

  export default {
    name: "addStaff",
    dicts: ['sys_user_sex','sys_political_status','sys_degree_name', 'sys_blood_type', 'sys_yes_no'],
    data() {
      const validIdNoResult = (rule, value, callback) => {
        validIdNo({idNo: value}).then(res => {
          if (res.result) {
            console.log("性别：" + res.sex);
            callback();
            this.form.sex = res.sex;
            this.form.age = res.age;
            this.form.birthDate = res.birthday;
          } else {
            callback(new Error("身份证号码验证未通过"));
          }
        });
      };
      const validPhoneResult = (rule, value, callback) => {
        console.log(value);
        validPhone({phone: value}).then(res => {
          if (res) {
            callback();
          } else {
            callback(new Error("联系电话验证未通过"));
          }
        });
      };
      return {
        activeName: "basic",
        // 省下拉框
        provinceOptions:[],
        // 民族下拉框
        nationOptions: [],
        battalionOptions: [],
        teamOptions: [],
        postOptions: [],
        payModeOptions: [{
          value: '按天'
        }, {
          value: '按月'
        }],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 标段编号
        institutionCode: undefined,
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          institutionCode: [
            { required: true, message: "标段编码不能为空", trigger: "blur" }
          ],
          idNo: [
            { required: true, message: "身份证号不能为空", trigger: "blur" },
            { max: 18, message: '身份证号最大长度为18个字符', trigger: 'blur'},
            { required: true, validator: validIdNoResult, trigger: "blur" }
          ],
          name: [
            { required: true, message: "姓名不能为空", trigger: "blur" },
            { max: 12, message: '姓名最大长度为12个字符', trigger: 'blur'},
          ],
          phone: [
            { required: true, message: "联系电话不能为空", trigger: "blur" },
            { max: 11, message: '联系电话最大长度为11个字符', trigger: 'blur'},
            { required: true, validator: validPhoneResult, trigger: "blur" }
          ],
        }
      }
    },
    methods: {
      // 显示弹框
      show(institutionCode, id) {
        this.reset();
        this.form.institutionCode = institutionCode;
        this.loadProvinceList();
        this.loadNationList();
        if (id == 0) {
          this.open = true;
          this.title = "添加劳务人员";
        } else {
          getStaff(id).then(response => {
            this.form = response.data;
            this.open = true;
            this.title = "修改劳务人员";
          });
        }
      },
      // 加载省
      loadProvinceList() {
        getProvinceList().then(response => {
          this.provinceOptions = response.data;
        });
      },
      // 加载民族
      loadNationList() {
        getNationList().then(response => {
          this.nationOptions = response.data;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          uuid: null,
          institutionCode: null,
          idNo: null,
          name: null,
          sex: "1",
          nation: null,
          birthDate: null,
          age: null,
          address: null,
          signingOrg: null,
          startExpiration: null,
          endExpiration: null,
          isExpiration: null,
          uploadCard: null,
          enterMode: null,
          photo: null,
          cardFrontPhoto: null,
          cardBackPhoto: null,
          recentPhoto: null,
          phone: null,
          politicalStatus: null,
          degreeName: null,
          nativePlace: null,
          bloodType: null,
          urgentContact: null,
          urgentTel: null,
          bankName: null,
          bankAccount: null,
          openBank: null,
          contractSigning: null,
          contractCode: null,
          contractStartExpiration: null,
          contractEndExpiration: null,
          contractPayday: null,
          contractPaySalary: null,
          contractPayMode: null,
          status: null,
          enterDate: null,
          exitDate: null,
          isMonitor: null,
          hatCode: null,
          battalionId: null,
          teamId: null,
          postId: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        };
        this.resetForm("form");
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateStaff(this.form).then(response => {
                this.$modal.msgSuccess("操作成功");
                this.open = false;
                this.$parent.getList();
              });
            } else {
              addStaff(this.form).then(response => {
                this.$modal.msgSuccess("操作成功");
                this.open = false;
                this.$parent.getList();
              });
            }
          }
        });
      },
    }
  }
</script>

<style lang="scss">
  .travel-app-add {
    padding-top: 20px;
  }
  input::-webkit-outer-spin-button,
  input::-webkit-inner-spin-button {
    -webkit-appearance: none;
  }
  input[type='number'] {
    -moz-appearance: textfield;
  }
</style>
